package com.chunayev.numerology.gui.action;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;


import com.chunayev.numerology.domain.PositionSummary;
import com.chunayev.numerology.util.Constants;
import com.chunayev.numerology.util.db.DBInitializer;
import com.sleepycat.je.DatabaseException;

/**
 * @author Sergey Chunayev
 */
public abstract class AbstractTokenAction extends AbstractAction implements
        ListSelectionListener {
    
    private SummaryTokenChangeNotifier summaryTokenChangeNotifier;
    
    /**
     * 
     */
    private static final long serialVersionUID = -1060693423503151146L;
    
    /**
     * 
     */
    public AbstractTokenAction() {
        super();
    }
    
    /**
     * @param actionName
     *            TODO
     */
    public AbstractTokenAction(final String actionName, final String keyName) {
        String name = Constants.RESOURCE_BUNDLE.getString(String.format(
                "action.%s.%s.token.name", actionName, keyName));
        String shortDescription = Constants.RESOURCE_BUNDLE.getString(String
                .format("action.%s.%s.token.short", actionName, keyName));
        putValue(Action.NAME, name);
        putValue(Action.SHORT_DESCRIPTION, shortDescription);
    }
    
    public AbstractTokenAction(final String actionName, final String keyName,
            final int mnemonicKey) {
        this(actionName, keyName);
        putValue(Action.MNEMONIC_KEY, mnemonicKey);
    }
    
    /*
     * (non-Javadoc)
     * @see
     * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    @Override
    public void actionPerformed(final ActionEvent e) {
        
        try {
            PositionSummary positionSummary = summaryTokenChangeNotifier
                    .getSummary();
            modifyPosition(positionSummary);
            DBInitializer.SUMMARY_PRIMARY_INDEX.put(positionSummary);
            getSummaryTokenChangeNotifier().update();
        } catch (DatabaseException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    /**
     * @return the summaryTokenChangeNotifier
     */
    public SummaryTokenChangeNotifier getSummaryTokenChangeNotifier() {
        return this.summaryTokenChangeNotifier;
    }
    
    public abstract void modifyPosition(PositionSummary positionSummary);
    
    /**
     * @param summaryTokenChangeNotifier
     *            the summaryTokenChangeNotifier to set
     */
    public void setSummaryTokenChangeNotifier(
            final SummaryTokenChangeNotifier summaryTokenChangeNotifier) {
        this.summaryTokenChangeNotifier = summaryTokenChangeNotifier;
    }
    
    /*
     * (non-Javadoc)
     * @see
     * javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event
     * .ListSelectionEvent)
     */
    @Override
    public void valueChanged(final ListSelectionEvent e) {
        if (!e.getValueIsAdjusting()) {
            ListSelectionModel model = (ListSelectionModel) e.getSource();
            int index = model.getAnchorSelectionIndex();
            System.out.println(index);
            if (index != -1) {
                setEnabled(true);
            } else {
                setEnabled(false);
                
            }
        }
    }
    
}
